package cn.wizzer.app.web.modules.controllers.platform.cms;

import cn.wizzer.app.cms.modules.models.CmsVideo;
import cn.wizzer.app.cms.modules.services.CmsVideoService;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * @author zl
 * @date 2020/6/23 17:39
 */
@At("/platform/cms/video")
@IocBean
public class CmsVideoController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private CmsVideoService videoService;

    @At("")
    @Ok("beetl:/platform/cms/video/index.html")
    @RequiresPermissions("cms.content.video")
    public void index() {
    }

    /**
     * 列表
     *
     * @param searchKeyword
     * @param searchDate
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.content.video")
    public Result data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("status") Integer status,
                       int pageNumber,
                       int pageSize) {
        Cnd cnd = Cnd.where("delFlag", "=", 0);
        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and("title", "like", "%" + searchKeyword + "%");
        }
        if (searchDate != null && searchDate.length > 0) {
            cnd.and("publishAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("publishAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        if (status != null) {
            cnd.and("disabled", "=", status);
        }
        cnd.orderBy("location", "DESC");
        return Result.success().addData(videoService.listPage(pageNumber, pageSize, cnd));
    }

    /**
     * 新增
     *
     * @param cmsVideo
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.content.video.edit")
    public Result add(@Param("..") CmsVideo cmsVideo) {
        CmsVideo oldCmsVideo = videoService.fetch(Cnd.where("title", "=", cmsVideo.getTitle()).and("delFlag", "=", 0));
        if (oldCmsVideo != null) {
            return Result.error(BizExceptionEnum.TITLE_EXISTS);
        }
        videoService.add(cmsVideo);
        return Result.success("添加成功");
    }

    /**
     * 编辑
     *
     * @param cmsVideo
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.content.video.edit")
    public Result edit(@Param("..") CmsVideo cmsVideo) {
        CmsVideo oldCmsVideo = videoService.fetch(Cnd.where("title", "=", cmsVideo.getTitle())
                .and("id", "!=", cmsVideo.getId())
                .and("delFlag", "=", 0));
        if (oldCmsVideo != null) {
            return Result.error(BizExceptionEnum.TITLE_EXISTS);
        }
        videoService.edit(cmsVideo);
        return Result.success("修改成功");
    }

    /**
     * 启、禁用
     *
     * @param id
     * @param status
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.content.video.edit")
    public Result disabled(@Param("id") String id,
                           @Param("status") Integer status) {
        videoService.update(Chain.make("disabled", status), Cnd.where("id", "=", id));
        videoService.clearCache();
        if(status == 1){
            return Result.success("禁用成功");
        }else {
            return Result.success("启用成功");
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.content.video.del")
    public Result del(@Param("ids") String[] ids) {
        videoService.update(Chain.make("delFlag", 1), Cnd.where("id", "IN", ids));
        videoService.clearCache();
        return Result.success("删除成功");
    }

}
